//
//  VolumeFooterView.m
//  JiaoX
//
//  Created by Northwan on 15/4/8.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "VolumeFooterView.h"
#define TEXT_SIZE  [DimensUtils getScaleX:12]

@interface VolumeFooterView()
@property(nonatomic,strong)UILabel *numLabelOne;
@property(nonatomic,strong)UILabel *tipsLabel;
@property(nonatomic,strong)UIImageView *arrowImageView;
@end

@implementation VolumeFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBounds:CGRectMake(0, 0, JXScreenW, 65/2)];
        NSString *numLabelTextOne = @"使用代金券";
        CGSize numLabelTextOneSize = [numLabelTextOne sizeWithFont:[UIFont systemFontOfSize:12]];
        _numLabelOne = [[UILabel alloc]init];
        [_numLabelOne setFont:[UIFont systemFontOfSize:TEXT_SIZE]];
        [_numLabelOne setText:numLabelTextOne];
        [_numLabelOne setTextColor:JXColor(102, 102, 102)];
        [_numLabelOne setFrame:CGRectMake(JXPublicInset, self.height/2 - numLabelTextOneSize.height/2, numLabelTextOneSize.width, numLabelTextOneSize.height)];
        [self addSubview:_numLabelOne];

        UIImage *arrowImg = [UIImage imageNamed:@"self_item_arrow"];
        
        NSString *tipsLabelText = @"未使用";
        CGSize tipsLabelTextSize = [tipsLabelText sizeWithFont:[UIFont systemFontOfSize:TEXT_SIZE]];
        _tipsLabel = [[UILabel alloc]init];
        [_tipsLabel setText:tipsLabelText];
        [_tipsLabel setFont:[UIFont systemFontOfSize:TEXT_SIZE]];
        [_tipsLabel setTextColor:JXThemeColor];
        [_tipsLabel setFrame:CGRectMake(self.width-tipsLabelTextSize.width-arrowImg.size.width-JXPublicInset*3, CGRectGetMinY(_numLabelOne.frame), tipsLabelTextSize.width, tipsLabelTextSize.height)];
        [self addSubview:_tipsLabel];
        
        _arrowImageView = [[UIImageView alloc]init];
        [_arrowImageView setImage:arrowImg];
        [_arrowImageView setFrame:CGRectMake(self.width-arrowImg.size.width-JXPublicInset*2, self.height/2-arrowImg.size.height/2, arrowImg.size.width, arrowImg.size.height)];
        [self addSubview:_arrowImageView];
    }
    return self;
}

-(void)setVolumeNum:(NSUInteger)num price:(NSUInteger)price{
    if(num != 0){//左边label显示x张可用,右边label显示未使用
        NSString *number = [NSString stringWithFormat:@"%lu",(unsigned long)num];
        NSString *numText = [NSString stringWithFormat:@"%@张可用",number];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:numText];
        [str addAttribute:NSForegroundColorAttributeName value:JXThemeColor range:NSMakeRange(0,number.length)];
        CGSize numLabelTextOneSize = [numText sizeWithFont:[UIFont systemFontOfSize:TEXT_SIZE]];
        [_numLabelOne setAttributedText:str];
        [_numLabelOne setFrame:CGRectMake(0, self.height/2 -numLabelTextOneSize.height/2, numLabelTextOneSize.width, numLabelTextOneSize.height)];
        
        [_tipsLabel setText:@"未使用"];
        UIImage *arrowImg = [UIImage imageNamed:@"self_item_arrow"];
        _tipsLabel.x = self.width-self.tipsLabel.width-arrowImg.size.width-JXPublicInset*3;
        _tipsLabel.userInteractionEnabled = NO;
        [_tipsLabel removeGestureRecognizer:[[UITapGestureRecognizer alloc]init]];
    }else{
        NSString *defaultText = @"使用代金券";
        CGSize numLabelTextOneSize = [defaultText sizeWithFont:[UIFont systemFontOfSize:TEXT_SIZE]];
        [_numLabelOne setText:defaultText];
        [_numLabelOne setFrame:CGRectMake(0, self.height/2 -numLabelTextOneSize.height/2, numLabelTextOneSize.width, numLabelTextOneSize.height)];
    }
    
    if(price != 0){//左边label显示已使用￥x,右边label显示取消
        NSString *defaultText = @"已使用";
        NSString *numText = [NSString stringWithFormat:@"已使用¥%lu",(unsigned long)price];
        int numTextLength = (int)numText.length;
        int defaultTextLenght = (int)defaultText.length;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:numText];
        [str addAttribute:NSForegroundColorAttributeName value:JXThemeColor range:NSMakeRange(3,numTextLength-defaultTextLenght)];
        CGSize numLabelTextOneSize = [numText sizeWithFont:[UIFont systemFontOfSize:TEXT_SIZE]];
        [_numLabelOne setAttributedText:str];
        [_numLabelOne setFrame:CGRectMake(0, self.height/2 -numLabelTextOneSize.height/2, numLabelTextOneSize.width, numLabelTextOneSize.height)];
        
        [_tipsLabel setText:@"取消"];
        UIImage *arrowImg = [UIImage imageNamed:@"self_item_arrow"];
        _tipsLabel.x = self.width-self.tipsLabel.width-arrowImg.size.width-JXPublicInset*3;
        _tipsLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCancle)];
        [_tipsLabel addGestureRecognizer:singleTap];
    }
}

/**
 * 取消现金券
 */
-(void)clickCancle{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"account_voucher_cancle" object:nil];
}

@end
